import Map from "../Map";

const { Tween, Handler, Event } = Laya;
/**
 * Direct 直射
 */
export class Direct {
	
	constructor(BulletClass) {
		this.BulletClass = BulletClass;
	}

	fire(fromX, fromY, toX, toY, width) {
		const BulletClass = this.BulletClass;
		let mapview = Map.getInstance();
		this.bulletInstance = BulletClass.getBullet();
		Map.getInstance().addChild(this.bulletInstance);
		let diffX = toX - fromX, diffY = toY - fromY;
		let dis = Math.sqrt(diffX*diffX + diffY*diffY);
		this.bulletInstance.pos(fromX, fromY);
		Tween.to(this.bulletInstance, { x: toX, y: toY }, parseInt(dis/2), null, Handler.create(this.bulletInstance, this.bulletInstance.destroy));
	}
}

/**
 * Scatter 散射
 */
export class Scatter {
	
	constructor(BulletClass) {
		this.BulletClass = BulletClass;
	}

	fire(fromX, fromY, radius, startAngle, endAngle, num) {

	}

}


const g = 2;
let vy = 0, vx = 30, vy0 = 0, t = 0, dt;

/**
 * Catapult 抛射
 */
export class Catapult {

	constructor(BulletClass) {
		this.BulletClass = BulletClass;
	}

	fire(fromX, fromY, toX, toY) {
		t = 0;
		let mapview = Map.getInstance();
		dt = (toX - fromX) / vx;
		vy = vy0 = (toY - fromY) / dt - g*dt;

		const BulletClass = this.BulletClass;
		this.bulletInstance = new BulletClass();
		this.bulletInstance.pos(fromX, fromY);
		mapview.addChild(this.bulletInstance);

		Laya.timer.frameLoop(1, this, this._flyLoop);
	}

	_flyLoop() {
		vy += g;
		this.bulletInstance.x += vx;
		this.bulletInstance.y += vy;
		if (++t >= dt) {
			Laya.timer.clear(this, this._flyLoop);
			this.bulletInstance.destroy();
		}
	}
	
}

/**
 * CloseIn 近战
 */
